var app = getApp();

var categories = new Array();
var multiArray = new Array();
var multiIndex = [0, 0];
var firstChoose = true;
var preMultiIndex = new Array();
var preMultiArray = new Array();

var curArticlePage = 1;
var curArticleCat = 0;
var curArticleSort = 1;

var articleSize = 20;

// pages/story/story.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    multiArray: multiArray,
    multiIndex: multiIndex,
    articles:[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var _this = this;
    init(_this);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var _this = this;
    init(_this);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var _this = this;
    createArticles(_this, curArticleCat, curArticleSort, curArticlePage+1, articleSize);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  initMultiArray:function(){
    var subMultiArray = new Array();
    subMultiArray.push("热门");

    for (var index in categories) {
      subMultiArray.push(categories[index].name);
    }
    multiArray = [subMultiArray, ['热门']];
    this.setData({ multiArray: multiArray });
  },
  bindMultiPickerChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      multiIndex: e.detail.value,
      articles:[]
    });

    firstChoose = true;

    var parenName = this.data.multiArray[0][e.detail.value[0]];
    var childName = this.data.multiArray[1][e.detail.value[1]];

    curArticleCat = getCurArticleCat(parenName,childName);
    curArticleSort = 2;
    curArticlePage = 1;
    if(curArticleCat == 0){
      curArticleSort = 1;
    }

    createArticles(this,curArticleCat,curArticleSort,curArticlePage,articleSize);
  },
  bindMultiPickerColumnChange: function (e) {
    if (firstChoose){
      firstChoose = false;
      preMultiArray = JSON.parse(JSON.stringify(this.data.multiArray));
      preMultiIndex = JSON.parse(JSON.stringify(this.data.multiIndex));
    }
    // console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;

    var curColumn = e.detail.column;
    if(curColumn == 0){
      var multiArrayChildren = getMultiArrayChildren(e.detail.value);
      data.multiArray[1] = multiArrayChildren;
    }

    this.setData(data);
  },
  bindMultiPickerCancel:function(e){
    var data = {
      multiArray: preMultiArray,
      multiIndex: preMultiIndex
    };
    this.setData(data);

    firstChoose = true;
  },
  bindToDetail:function(e){
    var id = e.currentTarget.id;

    var detailObj = { url:"/pages/article_detail/article_detail?id="+id};
    wx.navigateTo(detailObj);
  }
});

function getMultiArrayChildren(curIndex){
  if(curIndex == 0){
    return new Array();
  }
  curIndex = curIndex - 1;
  
  var curChindren = categories[curIndex].childDCategories;
  
  var curMultiArrayChildren = new Array();
  
  for(var chindIndex in curChindren){
    curMultiArrayChildren.push(curChindren[chindIndex].name);
  }

  return curMultiArrayChildren;
}


function init(_this){
  wx.request({
    url: app.globalData.requestUrl + '/article/categories',
    data: '',
    header: {},
    method: 'POST',
    dataType: 'json',
    responseType: 'text',
    success: function (res) {
      if (res.data.responseCode === "000") {
        categories = res.data.data;
        _this.initMultiArray(categories);
      }

    },
    fail: function (res) { },
    complete: function (res) { },
  });

  //重置数据
  curArticleCat = 0;
  curArticlePage = 1;
  curArticleSort = 1;
  _this.setData({ "articles": [], multiIndex: [0, 0], multiArray: new Array()});

  createArticles(_this, curArticleCat, curArticleSort,curArticlePage,articleSize);
}


function createArticles(_this, cat, sort, page,size) {
  var reqData = {request:
    {
      cat: [cat], sort: sort, page: page, size: size
    }
  };
  
  wx.request({
    url: app.globalData.requestUrl + '/article/list',
    data: JSON.stringify(reqData),
    header: {},
    method: 'POST',
    dataType: 'json',
    responseType: 'text',
    success: function (res) {
      var curArticles = new Array();
      if (res.data.responseCode === "000") {
        var items = res.data.data.items;
        curArticles = getCurArticles(items);

        curArticlePage = page;
        curArticleCat = cat;
        curArticleSort = sort;

        for(var _index in curArticles){
          _this.data.articles.push(curArticles[_index]);
        }

        curArticles = _this.data.articles;

        _this.setData({ "articles": curArticles });
      }
      
    },
    fail: function (res) { },
    complete: function (res) { },
  });
}

function getCurArticles(items){

  var curArticles = new Array();

  for(var index in items){
    var item = items[index];

    var hasCover = false;

    if(item.coverUrl){
      hasCover = true;
    }

    var curArtice = {id:item.id,title:item.title,content:item.content,
          summary:item.summary,showTime:item.showTime,coverUrl:item.coverUrl,hasCover:hasCover
          };
    curArticles.push(curArtice);
  }

  return curArticles;
}


function getCurArticleCat(parenName, childName){
  if(parenName === "热门"){
    return 0;
  }

  for (var index in categories){
      var curCategory = categories[index];
      if(curCategory.name === parenName){
        return getCurArticleCat4Child(curCategory.childDCategories,childName);
      }
  }
  return 0;
}

function getCurArticleCat4Child(childDCategories, childName){
  for (var index in childDCategories){
    var curCategory = childDCategories[index];
    if (curCategory.name === childName) {
      return curCategory.id;
    }
  }

  return 0;
}